package com.iraeta.controller;

import com.iraeta.service.etl.BpmEtlService;
import com.iraeta.service.etl.ErpEtlService;
import com.iraeta.service.etl.ErpGqEtlService;
import com.iraeta.service.sys.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@EnableScheduling
@EnableAsync
public class GlobalTask {
    private static final Logger logger = LoggerFactory.getLogger(GlobalTask.class);

    @Autowired
    FileService fileService;
    @Autowired
    ErpEtlService erpEtlService;
    @Autowired
    ErpGqEtlService erpGqEtlService;
    @Autowired
    BpmEtlService bpmEtlService;

    @Scheduled(cron ="0 0/2 * * * ?")
    @Async("cstaskexecutor")
    public void run() {
        try {
            //fileService.checkDjNewFile();
        }catch (Exception e){
            logger.error("[GlobalTask-Run]" + e.getMessage());
        }
    }

    @Scheduled(cron ="0 0/3 * * * ?")
    @Async("cstaskexecutor")
    public void processDjFile() {
        try {
            //fileService.processDjFile();
        }catch (Exception e){
            logger.error("[GlobalTask-ProcessDjFile]" + e.getMessage());
        }
    }

    @Scheduled(cron = "0 0 6,9,13,15,17,19,21 * * ?")
    public void processBpmData(){
        logger.info("processBpmData begin");
        try {
            bpmEtlService.processBpmSalXshtm();         //销售合同主表同步
        }catch (Exception e){
            logger.error("bpm销售合同主表同步错误:" + e.getMessage());
        }
        try {
            bpmEtlService.processBpmSalXshtc();         //销售合同表体数据同步
        }catch (Exception e){
            logger.error("bpm销售合同表体数据错误:" + e.getMessage());
        }
        try {
            bpmEtlService.processBpmSalXsddm();         //销售订单主表同步
        }catch (Exception e){
            logger.error("bpm销售订单主表同步错误:" + e.getMessage());
        }
        try {
            bpmEtlService.processBpmSalXsddc();         //销售订单表体数据同步
        }catch (Exception e){
            logger.error("bpm销售订单表体数据错误:" + e.getMessage());
        }
        try {
            bpmEtlService.processBpmErClbxdm();         //差旅报销主表同步
        }catch (Exception e){
            logger.error("bpm差旅报销主表同步错误:" + e.getMessage());
        }
        try {
            bpmEtlService.processBpmErRcfybxdm();         //其他费用报销数据同步
        }catch (Exception e){
            logger.error("bpm其他费用报销主表数据错误:" + e.getMessage());
        }
        logger.info("processBpmData end");
    }

    @Scheduled(cron = "0 0 7,10,12,14,16,18,22 * * ?")
    @Async("cstaskexecutor")
    public void processErpData() {
        logger.info("processErpData begin");
        try {
            erpEtlService.processErpSoMain();         //销售订单同步
        }catch (Exception e){
            logger.error("ERP销售订单同步错误:" + e.getMessage());
        }
        try {
            erpEtlService.processErpMomOrder();         //生产订单同步
        }catch (Exception e){
            logger.error("ERP产生品入库同步错误:" + e.getMessage());
        }
        try {
            erpEtlService.processErpRdrecordSc();         //生产入库通过
        }catch (Exception e){
            logger.error("ERP产生品入库同步错误:" + e.getMessage());
        }
        try {
            erpEtlService.processErpRdrecord01WX();         //外协入库通过
        }catch (Exception e){
            logger.error("ERP外协采购入库同步错误:" + e.getMessage());
        }
        try {
            erpEtlService.processErpRdrecord32();         //销售出库同步
        }catch (Exception e){
            logger.error("ERP销售出库同步错误:" + e.getMessage());
        }
        try {
            erpEtlService.processErpRdrecord11();         //领料出库同步
        }catch (Exception e){
            logger.error("ERP下料出库同步错误:" + e.getMessage());
        }
        //钢球的数据
        try {
            erpGqEtlService.processErpGqSoMain();         //钢球销售订单同步
        }catch (Exception e){
            logger.error("钢球ERP销售订单同步错误:" + e.getMessage());
        }
        try {
            erpGqEtlService.processErpgqRdrecord32();         //钢球销售出库单同步
        }catch (Exception e){
            logger.error("钢球ERP销售出库单同步错误:" + e.getMessage());
        }
        try {
            erpGqEtlService.processErpGqCurrentStock();         //钢球库存数据同步
        }catch (Exception e){
            logger.error("钢球ERP库存数据同步错误:" + e.getMessage());
        }
        logger.info("processErpData end");
    }
}
